<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>MyBatisPlus - 我的Hugo博客</title><meta name="Description" content=""><meta property="og:title" content="MyBatisPlus" />
<meta property="og:description" content="MyBatis Plus 国产的开源框架，基于 MyBatis
核心功能就是简化 MyBatis 的开发，提高效率。
MyBatis Plus 快速上手 Spring Boot(2.3.0) &#43; MyBatis Plus（国产的开源框架，并没有接入到 Spring 官方孵化器中）
1、创建 Maven 工程
2、pom.xml 引入 MyBatis Plus 的依赖
&lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt; &lt;version&gt;3.3.1.tmp&lt;/version&gt; &lt;/dependency&gt; 3、创建实体类
package com.southwind.mybatisplus.entity; import lombok.Data; @Data public class User { private Integer id; private String name; private Integer age; } 4、创建 Mapper 接口
package com.southwind.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.southwind.mybatisplus.entity.User; public interface UserMapper extends BaseMapper&lt;User&gt; { } 5、application.yml
spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF-8username:rootpassword:rootmybatis-plus:configuration:log-impl:org.apache.ibatis.logging.stdout.StdOutImpl6、启动类需要添加 @MapperScan(&ldquo;mapper所在的包&rdquo;)，否则无法加载 Mppaer bean。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://xls7771.gitee.io/posts/mybatisplus/" />
<meta property="article:published_time" content="2020-07-23T17:07:57+08:00" />
<meta property="article:modified_time" content="2020-07-23T17:07:57+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="MyBatisPlus"/>
<meta name="twitter:description" content="MyBatis Plus 国产的开源框架，基于 MyBatis
核心功能就是简化 MyBatis 的开发，提高效率。
MyBatis Plus 快速上手 Spring Boot(2.3.0) &#43; MyBatis Plus（国产的开源框架，并没有接入到 Spring 官方孵化器中）
1、创建 Maven 工程
2、pom.xml 引入 MyBatis Plus 的依赖
&lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt; &lt;version&gt;3.3.1.tmp&lt;/version&gt; &lt;/dependency&gt; 3、创建实体类
package com.southwind.mybatisplus.entity; import lombok.Data; @Data public class User { private Integer id; private String name; private Integer age; } 4、创建 Mapper 接口
package com.southwind.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.southwind.mybatisplus.entity.User; public interface UserMapper extends BaseMapper&lt;User&gt; { } 5、application.yml
spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF-8username:rootpassword:rootmybatis-plus:configuration:log-impl:org.apache.ibatis.logging.stdout.StdOutImpl6、启动类需要添加 @MapperScan(&ldquo;mapper所在的包&rdquo;)，否则无法加载 Mppaer bean。"/>
<meta name="application-name" content="我的Hugo博客">
<meta name="apple-mobile-web-app-title" content="我的Hugo博客"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://xls7771.gitee.io/posts/mybatisplus/" /><link rel="prev" href="https://xls7771.gitee.io/posts/mysql/" /><link rel="next" href="https://xls7771.gitee.io/posts/linux/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "MyBatisPlus",
        "inLanguage": "zh-cn",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/xls7771.gitee.io\/posts\/mybatisplus\/"
        },"genre": "posts","wordcount":  1020 ,
        "url": "https:\/\/xls7771.gitee.io\/posts\/mybatisplus\/","datePublished": "2020-07-23T17:07:57+08:00","dateModified": "2020-07-23T17:07:57+08:00","publisher": {
            "@type": "Organization",
            "name": "xls"},"author": {
                "@type": "Person",
                "name": "xls"
            },"description": ""
    }
    </script></head>
    <body header-desktop="" header-mobile=""><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="我的Hugo博客">我的Hugo博客</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="我的Hugo博客">我的Hugo博客</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">MyBatisPlus</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>xls</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-07-23">2020-07-23</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1020 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 5 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#mybatis-plus">MyBatis Plus</a></li>
        <li><a href="#mybatis-plus-快速上手">MyBatis Plus 快速上手</a></li>
        <li><a href="#常用注解">常用注解</a></li>
        <li><a href="#查询">查询</a></li>
        <li><a href="#自定义-sql多表关联查询">自定义 SQL（多表关联查询）</a></li>
        <li><a href="#添加">添加</a></li>
        <li><a href="#删除">删除</a></li>
        <li><a href="#修改">修改</a></li>
        <li><a href="#mybatisplus-自动生成">MyBatisPlus 自动生成</a></li>
        <li><a href="#spring-boot--mybatis-plus-打包应用直接发布-阿里云-上云">Spring Boot + MyBatis Plus 打包应用，直接发布 阿里云 上云</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h3 id="mybatis-plus">MyBatis Plus</h3>
<p>国产的开源框架，基于 MyBatis</p>
<p>核心功能就是简化 MyBatis 的开发，提高效率。</p>
<h3 id="mybatis-plus-快速上手">MyBatis Plus 快速上手</h3>
<p>Spring Boot(2.3.0) + MyBatis Plus（国产的开源框架，并没有接入到 Spring 官方孵化器中）</p>
<p>1、创建 Maven 工程</p>
<p>2、pom.xml 引入 MyBatis Plus 的依赖</p>
<div class="highlight"><pre class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>com.baomidou<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>mybatis-plus-boot-starter<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;version&gt;</span>3.3.1.tmp<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
</code></pre></div><p>3、创建实体类</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">age</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><p>4、创建 Mapper 接口</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.mapper</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.core.mapper.BaseMapper</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.southwind.mybatisplus.entity.User</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">UserMapper</span> <span class="kd">extends</span> <span class="n">BaseMapper</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="o">{</span>

<span class="o">}</span>
</code></pre></div><p>5、application.yml</p>
<div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml"><span class="k">spring</span><span class="p">:</span><span class="w">
</span><span class="w">  </span><span class="k">datasource</span><span class="p">:</span><span class="w">
</span><span class="w">    </span><span class="k">driver-class-name</span><span class="p">:</span><span class="w"> </span>com.mysql.cj.jdbc.Driver<span class="w">
</span><span class="w">    </span><span class="k">url</span><span class="p">:</span><span class="w"> </span>jdbc<span class="p">:</span>mysql<span class="p">:</span>//localhost<span class="p">:</span><span class="m">3306</span>/db<span class="p">?</span>useUnicode=<span class="kc">true</span><span class="cp">&amp;characterEncoding=UTF-8</span><span class="w">
</span><span class="w">    </span><span class="k">username</span><span class="p">:</span><span class="w"> </span>root<span class="w">
</span><span class="w">    </span><span class="k">password</span><span class="p">:</span><span class="w"> </span>root<span class="w">
</span><span class="w"></span><span class="k">mybatis-plus</span><span class="p">:</span><span class="w">
</span><span class="w">  </span><span class="k">configuration</span><span class="p">:</span><span class="w">
</span><span class="w">    </span><span class="k">log-impl</span><span class="p">:</span><span class="w"> </span>org.apache.ibatis.logging.stdout.StdOutImpl<span class="w">
</span></code></pre></div><p>6、启动类需要添加 @MapperScan(&ldquo;mapper所在的包&rdquo;)，否则无法加载 Mppaer bean。</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.mybatis.spring.annotation.MapperScan</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.SpringApplication</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.autoconfigure.SpringBootApplication</span><span class="o">;</span>

<span class="nd">@SpringBootApplication</span>
<span class="nd">@MapperScan</span><span class="o">(</span><span class="s">&#34;com.southwind.mybatisplus.mapper&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MybatisplusApplication</span> <span class="o">{</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">SpringApplication</span><span class="o">.</span><span class="na">run</span><span class="o">(</span><span class="n">MybatisplusApplication</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">args</span><span class="o">);</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></div><p>7、测试</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.mapper</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.junit.jupiter.api.Test</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.beans.factory.annotation.Autowired</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.test.context.SpringBootTest</span><span class="o">;</span>

<span class="nd">@SpringBootTest</span>
<span class="kd">class</span> <span class="nc">UserMapperTest</span> <span class="o">{</span>

    <span class="nd">@Autowired</span>
    <span class="kd">private</span> <span class="n">UserMapper</span> <span class="n">mapper</span><span class="o">;</span>

    <span class="nd">@Test</span>
    <span class="kt">void</span> <span class="nf">test</span><span class="o">(){</span>
        <span class="n">mapper</span><span class="o">.</span><span class="na">selectList</span><span class="o">(</span><span class="kc">null</span><span class="o">).</span><span class="na">forEach</span><span class="o">(</span><span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">::</span><span class="n">println</span><span class="o">);</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></div><h3 id="常用注解">常用注解</h3>
<blockquote>
<p>@TableName</p>
</blockquote>
<p>映射数据库的表名</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.TableName</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="nd">@TableName</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;user&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Account</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">age</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><blockquote>
<p>@TableId</p>
</blockquote>
<p>设置主键映射，value 映射主键字段名</p>
<p>type 设置主键类型，主键的生成策略，</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="n">AUTO</span><span class="o">(</span><span class="n">0</span><span class="o">),</span>
<span class="n">NONE</span><span class="o">(</span><span class="n">1</span><span class="o">),</span>
<span class="n">INPUT</span><span class="o">(</span><span class="n">2</span><span class="o">),</span>
<span class="n">ASSIGN_ID</span><span class="o">(</span><span class="n">3</span><span class="o">),</span>
<span class="n">ASSIGN_UUID</span><span class="o">(</span><span class="n">4</span><span class="o">),</span>
<span class="cm">/** @deprecated */</span>
<span class="nd">@Deprecated</span>
<span class="n">ID_WORKER</span><span class="o">(</span><span class="n">3</span><span class="o">),</span>
<span class="cm">/** @deprecated */</span>
<span class="nd">@Deprecated</span>
<span class="n">ID_WORKER_STR</span><span class="o">(</span><span class="n">3</span><span class="o">),</span>
<span class="cm">/** @deprecated */</span>
<span class="nd">@Deprecated</span>
<span class="n">UUID</span><span class="o">(</span><span class="n">4</span><span class="o">);</span>
</code></pre></div><table>
<thead>
<tr>
<th>值</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>AUTO</td>
<td>数据库自增</td>
</tr>
<tr>
<td>NONE</td>
<td>MP set 主键，雪花算法实现</td>
</tr>
<tr>
<td>INPUT</td>
<td>需要开发者手动赋值</td>
</tr>
<tr>
<td>ASSIGN_ID</td>
<td>MP 分配 ID，Long、Integer、String</td>
</tr>
<tr>
<td>ASSIGN_UUID</td>
<td>分配 UUID，Strinig</td>
</tr>
</tbody>
</table>
<p>INPUT 如果开发者没有手动赋值，则数据库通过自增的方式给主键赋值，如果开发者手动赋值，则存入该值。</p>
<p>AUTO 默认就是数据库自增，开发者无需赋值。</p>
<p>ASSIGN_ID MP 自动赋值，雪花算法。</p>
<p>ASSIGN_UUID 主键的数据类型必须是 String，自动生成 UUID 进行赋值</p>
<blockquote>
<p>@TableField</p>
</blockquote>
<p>映射非主键字段，value 映射字段名</p>
<p>exist 表示是否为数据库字段 false，如果实体类中的成员变量在数据库中没有对应的字段，则可以使用 exist，VO、DTO</p>
<p>select 表示是否查询该字段</p>
<p>fill 表示是否自动填充，将对象存入数据库的时候，由 MyBatis Plus 自动给某些字段赋值，create_time、update_time</p>
<p>1、给表添加 create_time、update_time 字段</p>
<p>2、实体类中添加成员变量</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.FieldFill</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.TableField</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.TableId</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.TableName</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="nd">@TableName</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;user&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
    <span class="nd">@TableId</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">id</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;name&#34;</span><span class="o">,</span><span class="n">select</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">title</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">age</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">exist</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">gender</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">createTime</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT_UPDATE</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">updateTime</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><p>3、创建自动填充处理器</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.handler</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.core.handlers.MetaObjectHandler</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.ibatis.reflection.MetaObject</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Component</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>

<span class="nd">@Component</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyMetaObjectHandler</span> <span class="kd">implements</span> <span class="n">MetaObjectHandler</span> <span class="o">{</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">insertFill</span><span class="o">(</span><span class="n">MetaObject</span> <span class="n">metaObject</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">setFieldValByName</span><span class="o">(</span><span class="s">&#34;createTime&#34;</span><span class="o">,</span><span class="k">new</span> <span class="n">Date</span><span class="o">(),</span><span class="n">metaObject</span><span class="o">);</span>
        <span class="k">this</span><span class="o">.</span><span class="na">setFieldValByName</span><span class="o">(</span><span class="s">&#34;updateTime&#34;</span><span class="o">,</span><span class="k">new</span> <span class="n">Date</span><span class="o">(),</span><span class="n">metaObject</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">updateFill</span><span class="o">(</span><span class="n">MetaObject</span> <span class="n">metaObject</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">setFieldValByName</span><span class="o">(</span><span class="s">&#34;updateTime&#34;</span><span class="o">,</span><span class="k">new</span> <span class="n">Date</span><span class="o">(),</span><span class="n">metaObject</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div><blockquote>
<p>@Version</p>
</blockquote>
<p>标记乐观锁，通过 version 字段来保证数据的安全性，当修改数据的时候，会以 version 作为条件，当条件成立的时候才会修改成功。</p>
<p>version = 2</p>
<p>线程 1:update &hellip; set version = 2  where version = 1</p>
<p>线程2 ：update &hellip; set version = 2 where version = 1</p>
<p>1、数据库表添加 version 字段，默认值为 1</p>
<p>2、实体类添加 version 成员变量，并且添加 @Version</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.*</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="nd">@TableName</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;user&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
    <span class="nd">@TableId</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">id</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;name&#34;</span><span class="o">,</span><span class="n">select</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">title</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">age</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">exist</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">gender</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">createTime</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT_UPDATE</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">updateTime</span><span class="o">;</span>
    <span class="nd">@Version</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">version</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><p>3、注册配置类</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.config</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.context.annotation.Bean</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.context.annotation.Configuration</span><span class="o">;</span>

<span class="nd">@Configuration</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyBatisPlusConfig</span> <span class="o">{</span>
    
    <span class="nd">@Bean</span>
    <span class="kd">public</span> <span class="n">OptimisticLockerInterceptor</span> <span class="nf">optimisticLockerInterceptor</span><span class="o">(){</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">OptimisticLockerInterceptor</span><span class="o">();</span>
    <span class="o">}</span>
    
<span class="o">}</span>
</code></pre></div><blockquote>
<p>@EnumValue</p>
</blockquote>
<p>1、通用枚举类注解，将数据库字段映射成实体类的枚举类型成员变量</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.enums</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.EnumValue</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">enum</span> <span class="n">StatusEnum</span> <span class="o">{</span>
    <span class="n">WORK</span><span class="o">(</span><span class="n">1</span><span class="o">,</span><span class="s">&#34;上班&#34;</span><span class="o">),</span>
    <span class="n">REST</span><span class="o">(</span><span class="n">0</span><span class="o">,</span><span class="s">&#34;休息&#34;</span><span class="o">);</span>

    <span class="n">StatusEnum</span><span class="o">(</span><span class="n">Integer</span> <span class="n">code</span><span class="o">,</span> <span class="n">String</span> <span class="n">msg</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">code</span> <span class="o">=</span> <span class="n">code</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">msg</span> <span class="o">=</span> <span class="n">msg</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@EnumValue</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">code</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">msg</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.*</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.southwind.mybatisplus.enums.StatusEnum</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="nd">@TableName</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;user&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
    <span class="nd">@TableId</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">id</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;name&#34;</span><span class="o">,</span><span class="n">select</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">title</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">age</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">exist</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">gender</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">createTime</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT_UPDATE</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">updateTime</span><span class="o">;</span>
    <span class="nd">@Version</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">version</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">StatusEnum</span> <span class="n">status</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><p>application.yml</p>
<div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml"><span class="k">type-enums-package</span><span class="p">:</span><span class="w"> 
</span><span class="w">  </span>com.southwind.mybatisplus.enums<span class="w">
</span></code></pre></div><p>2、实现接口</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.enums</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.core.enums.IEnum</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">enum</span> <span class="n">AgeEnum</span> <span class="kd">implements</span> <span class="n">IEnum</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">&gt;</span> <span class="o">{</span>
    <span class="n">ONE</span><span class="o">(</span><span class="n">1</span><span class="o">,</span><span class="s">&#34;一岁&#34;</span><span class="o">),</span>
    <span class="n">TWO</span><span class="o">(</span><span class="n">2</span><span class="o">,</span><span class="s">&#34;两岁&#34;</span><span class="o">),</span>
    <span class="n">THREE</span><span class="o">(</span><span class="n">3</span><span class="o">,</span><span class="s">&#34;三岁&#34;</span><span class="o">);</span>

    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">code</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">msg</span><span class="o">;</span>

    <span class="n">AgeEnum</span><span class="o">(</span><span class="n">Integer</span> <span class="n">code</span><span class="o">,</span> <span class="n">String</span> <span class="n">msg</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">code</span> <span class="o">=</span> <span class="n">code</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">msg</span> <span class="o">=</span> <span class="n">msg</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="n">Integer</span> <span class="nf">getValue</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">code</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div><blockquote>
<p>@TableLogic</p>
</blockquote>
<p>映射逻辑删除</p>
<p>1、数据表添加 deleted 字段</p>
<p>2、实体类添加注解</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.*</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.southwind.mybatisplus.enums.AgeEnum</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.southwind.mybatisplus.enums.StatusEnum</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="nd">@TableName</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;user&#34;</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
    <span class="nd">@TableId</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">id</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;name&#34;</span><span class="o">,</span><span class="n">select</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">title</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">AgeEnum</span> <span class="n">age</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">exist</span> <span class="o">=</span> <span class="kc">false</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">gender</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">createTime</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">fill</span> <span class="o">=</span> <span class="n">FieldFill</span><span class="o">.</span><span class="na">INSERT_UPDATE</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">Date</span> <span class="n">updateTime</span><span class="o">;</span>
    <span class="nd">@Version</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">version</span><span class="o">;</span>
    <span class="nd">@TableField</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">&#34;status&#34;</span><span class="o">)</span>
    <span class="kd">private</span> <span class="n">StatusEnum</span> <span class="n">statusEnum</span><span class="o">;</span>
    <span class="nd">@TableLogic</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">deleted</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><p>3、application.yml 添加配置</p>
<div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml"><span class="k">global-config</span><span class="p">:</span><span class="w">
</span><span class="w">  </span><span class="k">db-config</span><span class="p">:</span><span class="w">
</span><span class="w">    </span><span class="k">logic-not-delete-value</span><span class="p">:</span><span class="w"> </span><span class="m">0</span><span class="w">
</span><span class="w">    </span><span class="k">logic-delete-value</span><span class="p">:</span><span class="w"> </span><span class="m">1</span><span class="w">
</span></code></pre></div><h3 id="查询">查询</h3>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="c1">//mapper.selectList(null);
</span><span class="c1"></span><span class="n">QueryWrapper</span> <span class="n">wrapper</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QueryWrapper</span><span class="o">();</span>
<span class="c1">//        Map&lt;String,Object&gt; map = new HashMap&lt;&gt;();
</span><span class="c1">//        map.put(&#34;name&#34;,&#34;小红&#34;);
</span><span class="c1">//        map.put(&#34;age&#34;,3);
</span><span class="c1">//        wrapper.allEq(map);
</span><span class="c1">//        wrapper.gt(&#34;age&#34;,2);
</span><span class="c1">//        wrapper.ne(&#34;name&#34;,&#34;小红&#34;);
</span><span class="c1">//        wrapper.ge(&#34;age&#34;,2);
</span><span class="c1"></span>
<span class="c1">//like &#39;%小&#39;
</span><span class="c1">//        wrapper.likeLeft(&#34;name&#34;,&#34;小&#34;);
</span><span class="c1">//like &#39;小%&#39;
</span><span class="c1">//        wrapper.likeRight(&#34;name&#34;,&#34;小&#34;);
</span><span class="c1"></span>
<span class="c1">//inSQL
</span><span class="c1">//        wrapper.inSql(&#34;id&#34;,&#34;select id from user where id &lt; 10&#34;);
</span><span class="c1">//        wrapper.inSql(&#34;age&#34;,&#34;select age from user where age &gt; 3&#34;);
</span><span class="c1"></span>
<span class="c1">//        wrapper.orderByDesc(&#34;age&#34;);
</span><span class="c1"></span>
<span class="c1">//        wrapper.orderByAsc(&#34;age&#34;);
</span><span class="c1">//        wrapper.having(&#34;id &gt; 8&#34;);
</span><span class="c1"></span>
<span class="n">mapper</span><span class="o">.</span><span class="na">selectList</span><span class="o">(</span><span class="n">wrapper</span><span class="o">).</span><span class="na">forEach</span><span class="o">(</span><span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">::</span><span class="n">println</span><span class="o">);</span>
</code></pre></div><div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="c1">//        System.out.println(mapper.selectById(7));
</span><span class="c1">//        mapper.selectBatchIds(Arrays.asList(7,8,9)).forEach(System.out::println);
</span><span class="c1"></span>
<span class="c1">//Map 只能做等值判断，逻辑判断需要使用 Wrapper 来处理
</span><span class="c1">//        Map&lt;String,Object&gt; map = new HashMap&lt;&gt;();
</span><span class="c1">//        map.put(&#34;id&#34;,7);
</span><span class="c1">//        mapper.selectByMap(map).forEach(System.out::println);
</span><span class="c1"></span>
<span class="n">QueryWrapper</span> <span class="n">wrapper</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QueryWrapper</span><span class="o">();</span>
<span class="n">wrapper</span><span class="o">.</span><span class="na">eq</span><span class="o">(</span><span class="s">&#34;id&#34;</span><span class="o">,</span><span class="n">7</span><span class="o">);</span>
<span class="c1">////        System.out.println(mapper.selectCount(wrapper));
</span><span class="c1">//
</span><span class="c1">//        //将查询的结果集封装到Map中
</span><span class="c1">//        mapper.selectMaps(wrapper).forEach(System.out::println);
</span><span class="c1">//        System.out.println(&#34;-------------------&#34;);
</span><span class="c1">//        mapper.selectList(wrapper).forEach(System.out::println);
</span><span class="c1"></span>
<span class="c1">//分页查询
</span><span class="c1">//        Page&lt;User&gt; page = new Page&lt;&gt;(2,2);
</span><span class="c1">//        Page&lt;User&gt; result = mapper.selectPage(page,null);
</span><span class="c1">//        System.out.println(result.getSize());
</span><span class="c1">//        System.out.println(result.getTotal());
</span><span class="c1">//        result.getRecords().forEach(System.out::println);
</span><span class="c1"></span>
<span class="c1">//        Page&lt;Map&lt;String,Object&gt;&gt; page = new Page&lt;&gt;(1,2);
</span><span class="c1">//        mapper.selectMapsPage(page,null).getRecords().forEach(System.out::println);
</span><span class="c1"></span>
<span class="c1">//        mapper.selectObjs(null).forEach(System.out::println);
</span><span class="c1"></span>

<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">mapper</span><span class="o">.</span><span class="na">selectOne</span><span class="o">(</span><span class="n">wrapper</span><span class="o">));</span>
</code></pre></div><h3 id="自定义-sql多表关联查询">自定义 SQL（多表关联查询）</h3>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.entity</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">lombok.Data</span><span class="o">;</span>

<span class="nd">@Data</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ProductVO</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">category</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">count</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">description</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">Integer</span> <span class="n">userId</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">userName</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div><div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus.mapper</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.core.mapper.BaseMapper</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.southwind.mybatisplus.entity.ProductVO</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.southwind.mybatisplus.entity.User</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.ibatis.annotations.Select</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">UserMapper</span> <span class="kd">extends</span> <span class="n">BaseMapper</span><span class="o">&lt;</span><span class="n">User</span><span class="o">&gt;</span> <span class="o">{</span>
    <span class="nd">@Select</span><span class="o">(</span><span class="s">&#34;select p.*,u.name userName from product p,user u where p.user_id = u.id and u.id = #{id}&#34;</span><span class="o">)</span>
    <span class="n">List</span><span class="o">&lt;</span><span class="n">ProductVO</span><span class="o">&gt;</span> <span class="nf">productList</span><span class="o">(</span><span class="n">Integer</span> <span class="n">id</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div><h3 id="添加">添加</h3>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="n">User</span> <span class="n">user</span> <span class="o">=</span> <span class="k">new</span> <span class="n">User</span><span class="o">();</span>
<span class="n">user</span><span class="o">.</span><span class="na">setTitle</span><span class="o">(</span><span class="s">&#34;小明&#34;</span><span class="o">);</span>
<span class="n">user</span><span class="o">.</span><span class="na">setAge</span><span class="o">(</span><span class="n">22</span><span class="o">);</span>
<span class="n">mapper</span><span class="o">.</span><span class="na">insert</span><span class="o">(</span><span class="n">user</span><span class="o">);</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">user</span><span class="o">);</span>
</code></pre></div><h3 id="删除">删除</h3>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="c1">//mapper.deleteById(1);
</span><span class="c1">//        mapper.deleteBatchIds(Arrays.asList(7,8));
</span><span class="c1">//        QueryWrapper wrapper = new QueryWrapper();
</span><span class="c1">//        wrapper.eq(&#34;age&#34;,14);
</span><span class="c1">//        mapper.delete(wrapper);
</span><span class="c1"></span>
<span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">&gt;</span> <span class="n">map</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;();</span>
<span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="s">&#34;id&#34;</span><span class="o">,</span><span class="n">10</span><span class="o">);</span>
<span class="n">mapper</span><span class="o">.</span><span class="na">deleteByMap</span><span class="o">(</span><span class="n">map</span><span class="o">);</span>
</code></pre></div><h3 id="修改">修改</h3>
<pre><code>//        //update ... version = 3 where version = 2
//        User user = mapper.selectById(7);
//        user.setTitle(&quot;一号&quot;);
//
//        //update ... version = 3 where version = 2
//        User user1 = mapper.selectById(7);
//        user1.setTitle(&quot;二号&quot;);
//
//        mapper.updateById(user1);
//        mapper.updateById(user);

User user = mapper.selectById(1);
user.setTitle(&quot;小红&quot;);
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq(&quot;age&quot;,22);
mapper.update(user,wrapper);
</code></pre><h3 id="mybatisplus-自动生成">MyBatisPlus 自动生成</h3>
<p>根据数据表自动生成实体类、Mapper、Service、ServiceImpl、Controller</p>
<p>1、pom.xml 导入 MyBatis Plus Generator</p>
<div class="highlight"><pre class="chroma"><code class="language-xml" data-lang="xml"><span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>com.baomidou<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>mybatis-plus-generator<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;version&gt;</span>3.3.1.tmp<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>

<span class="nt">&lt;dependency&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>org.apache.velocity<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>velocity<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;version&gt;</span>1.7<span class="nt">&lt;/version&gt;</span>
<span class="nt">&lt;/dependency&gt;</span>
</code></pre></div><p>Velocity（默认）、Freemarker、Beetl</p>
<p>2、启动类</p>
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"><span class="kn">package</span> <span class="nn">com.southwind.mybatisplus</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.annotation.DbType</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.generator.AutoGenerator</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.generator.config.DataSourceConfig</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.generator.config.GlobalConfig</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.generator.config.PackageConfig</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.generator.config.StrategyConfig</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.baomidou.mybatisplus.generator.config.rules.NamingStrategy</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Main</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">//创建generator对象
</span><span class="c1"></span>        <span class="n">AutoGenerator</span> <span class="n">autoGenerator</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AutoGenerator</span><span class="o">();</span>
        <span class="c1">//数据源
</span><span class="c1"></span>        <span class="n">DataSourceConfig</span> <span class="n">dataSourceConfig</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DataSourceConfig</span><span class="o">();</span>
        <span class="n">dataSourceConfig</span><span class="o">.</span><span class="na">setDbType</span><span class="o">(</span><span class="n">DbType</span><span class="o">.</span><span class="na">MYSQL</span><span class="o">);</span>
        <span class="n">dataSourceConfig</span><span class="o">.</span><span class="na">setUrl</span><span class="o">(</span><span class="s">&#34;jdbc:mysql://ip:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&#34;</span><span class="o">);</span>
        <span class="n">dataSourceConfig</span><span class="o">.</span><span class="na">setUsername</span><span class="o">(</span><span class="s">&#34;root&#34;</span><span class="o">);</span>
        <span class="n">dataSourceConfig</span><span class="o">.</span><span class="na">setPassword</span><span class="o">(</span><span class="s">&#34;root&#34;</span><span class="o">);</span>
        <span class="n">dataSourceConfig</span><span class="o">.</span><span class="na">setDriverName</span><span class="o">(</span><span class="s">&#34;com.mysql.cj.jdbc.Driver&#34;</span><span class="o">);</span>
        <span class="n">autoGenerator</span><span class="o">.</span><span class="na">setDataSource</span><span class="o">(</span><span class="n">dataSourceConfig</span><span class="o">);</span>
        <span class="c1">//全局配置
</span><span class="c1"></span>        <span class="n">GlobalConfig</span> <span class="n">globalConfig</span> <span class="o">=</span> <span class="k">new</span> <span class="n">GlobalConfig</span><span class="o">();</span>
        <span class="n">globalConfig</span><span class="o">.</span><span class="na">setOutputDir</span><span class="o">(</span><span class="n">System</span><span class="o">.</span><span class="na">getProperty</span><span class="o">(</span><span class="s">&#34;user.dir&#34;</span><span class="o">)+</span><span class="s">&#34;/src/main/java&#34;</span><span class="o">);</span>
        <span class="n">globalConfig</span><span class="o">.</span><span class="na">setOpen</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
        <span class="n">globalConfig</span><span class="o">.</span><span class="na">setAuthor</span><span class="o">(</span><span class="s">&#34;southwind&#34;</span><span class="o">);</span>
        <span class="n">globalConfig</span><span class="o">.</span><span class="na">setServiceName</span><span class="o">(</span><span class="s">&#34;%sService&#34;</span><span class="o">);</span>
        <span class="n">autoGenerator</span><span class="o">.</span><span class="na">setGlobalConfig</span><span class="o">(</span><span class="n">globalConfig</span><span class="o">);</span>
        <span class="c1">//包信息
</span><span class="c1"></span>        <span class="n">PackageConfig</span> <span class="n">packageConfig</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PackageConfig</span><span class="o">();</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setParent</span><span class="o">(</span><span class="s">&#34;com.southwind.mybatisplus&#34;</span><span class="o">);</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setModuleName</span><span class="o">(</span><span class="s">&#34;generator&#34;</span><span class="o">);</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setController</span><span class="o">(</span><span class="s">&#34;controller&#34;</span><span class="o">);</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setService</span><span class="o">(</span><span class="s">&#34;service&#34;</span><span class="o">);</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setServiceImpl</span><span class="o">(</span><span class="s">&#34;service.impl&#34;</span><span class="o">);</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setMapper</span><span class="o">(</span><span class="s">&#34;mapper&#34;</span><span class="o">);</span>
        <span class="n">packageConfig</span><span class="o">.</span><span class="na">setEntity</span><span class="o">(</span><span class="s">&#34;entity&#34;</span><span class="o">);</span>
        <span class="n">autoGenerator</span><span class="o">.</span><span class="na">setPackageInfo</span><span class="o">(</span><span class="n">packageConfig</span><span class="o">);</span>
        <span class="c1">//配置策略
</span><span class="c1"></span>        <span class="n">StrategyConfig</span> <span class="n">strategyConfig</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StrategyConfig</span><span class="o">();</span>
        <span class="n">strategyConfig</span><span class="o">.</span><span class="na">setEntityLombokModel</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
        <span class="n">strategyConfig</span><span class="o">.</span><span class="na">setNaming</span><span class="o">(</span><span class="n">NamingStrategy</span><span class="o">.</span><span class="na">underline_to_camel</span><span class="o">);</span>
        <span class="n">strategyConfig</span><span class="o">.</span><span class="na">setColumnNaming</span><span class="o">(</span><span class="n">NamingStrategy</span><span class="o">.</span><span class="na">underline_to_camel</span><span class="o">);</span>
        <span class="n">autoGenerator</span><span class="o">.</span><span class="na">setStrategy</span><span class="o">(</span><span class="n">strategyConfig</span><span class="o">);</span>

        <span class="n">autoGenerator</span><span class="o">.</span><span class="na">execute</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div><h3 id="spring-boot--mybatis-plus-打包应用直接发布-阿里云-上云">Spring Boot + MyBatis Plus 打包应用，直接发布 阿里云 上云</h3>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-07-23</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://xls7771.gitee.io/posts/mybatisplus/" data-title="MyBatisPlus"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://xls7771.gitee.io/posts/mybatisplus/"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Buffer" data-sharer="buffer" data-url="https://xls7771.gitee.io/posts/mybatisplus/" data-title="MyBatisPlus"><i class="fab fa-buffer fa-fw"></i></a><a href="javascript:void(0);" title="分享到 百度" data-sharer="baidu" data-url="https://xls7771.gitee.io/posts/mybatisplus/" data-title="MyBatisPlus"><i data-svg-src="/lib/simple-icons/icons/baidu.min.svg"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/mysql/" class="prev" rel="prev" title="MySQL"><i class="fas fa-angle-left fa-fw"></i>MySQL</a>
            <a href="/posts/linux/" class="next" rel="next" title="Linux">Linux<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.74.2">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">xls</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/autocomplete/autocomplete.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"search":{"highlightTag":"em","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":30}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
